package com.app.couponsharelife.ui.main.sort

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.app.couponsharelife.R
import com.app.couponsharelife.base.LazyFragment
import com.app.couponsharelife.data.bean.HomeClassBean
import com.app.couponsharelife.data.bean.RemoteResultBean
import com.app.couponsharelife.data.remote.retrofit.RetrofitWorker
import com.app.couponsharelife.data.remote.retrofit.exception.OtherException
import com.app.couponsharelife.manage.Contacts
import com.app.couponsharelife.ui.main.home.search.HomeSearchActivity
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.observers.DisposableObserver
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.fragment_newsortclass.*
import kotlinx.android.synthetic.main.popup_homeclass_rvitm.view.*
import java.util.HashMap

/**
 *Created by xct on 2019/4/24
 *Description:分类页面二级分类页
 */
class NewSortClassFragment:LazyFragment() {
    private var classList= arrayListOf<HomeClassBean>()
    private var myHomeClassBean:HomeClassBean?=null
    private var myClassAdapter:MyClassAdapter?=null
    private var favoritesID=""
    companion object {
        fun newInstance(homeClassBean: HomeClassBean): NewSortClassFragment {
            val fragment = NewSortClassFragment()
            val bundle= Bundle()
            bundle.putParcelable("Data",homeClassBean)
            fragment.arguments =bundle
            return fragment
        }
    }
    override val viewLayoutId: Int
        get() = R.layout.fragment_newsortclass

    override fun initViews() {
        myHomeClassBean=arguments!!.getParcelable("Data")
        if (myHomeClassBean!=null) {
            favoritesID = (myHomeClassBean!!.id ?: 0).toString()
            newSortClass_tvName.text=myHomeClassBean!!.name
            newSortClass_rv.layoutManager= GridLayoutManager(context,3)
            myClassAdapter=MyClassAdapter()
            newSortClass_rv.adapter=myClassAdapter
        }
    }

    override fun lazyLoad() {
        if (favoritesID.isNotEmpty()){
            getClassData()
        }
    }
    private fun getClassData(){
        showProgressDialog("加载中...")
        val map= HashMap<String,String>()
        map["firstId"]=favoritesID
        RetrofitWorker.retrofitWorker.getHomeTwoClass(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : DisposableObserver<RemoteResultBean<List<HomeClassBean>>>(){
                    override fun onComplete() {

                    }

                    override fun onNext(it: RemoteResultBean<List<HomeClassBean>>) {
                        if (it.code==200){
                            if (it.data!!.isNotEmpty()){
                                classList.clear()
                                classList.addAll(it.data!!)
                                myClassAdapter!!.notifyDataSetChanged()
                            }
                        }
                        dismissProgressDialog()
                    }

                    override fun onError(e: Throwable) {
                        dismissProgressDialog()
                        if (e is OtherException){
                            showToastMsg(e.msg)
                        }else{
                            showToastMsg("网络加载出错了！")
                        }
                    }

                })
    }
    private inner class MyClassAdapter: RecyclerView.Adapter<RecyclerView.ViewHolder>(){
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
            val view = LayoutInflater.from(context).inflate(R.layout.popup_homeclass_rvitm, parent,false)
            return MyClassHolder(view)
        }

        override fun getItemCount(): Int {
            return classList.size
        }

        override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
            if (classList.isNotEmpty()){
                if (holder is MyClassHolder){
                    val classBean=classList[position]
                    Glide.with(context!!).asBitmap()
                            .apply(RequestOptions().error(R.drawable.ic_app_place_holder3))
                            .load(Contacts.NEWREMOTE_IMAGE_URL +classBean.bgImg)
                            .into(holder.imageView!!)
                    holder.tvName!!.text=classBean.name?:""
                    holder.itemView.setOnClickListener {
                        //holder.tvName!!.setTextColor(context!!.resources.getColor(R.color.red_ff038))
                        val intent= Intent(context, HomeSearchActivity::class.java)
                        intent.putExtra("KeyWord",classBean.keyWord?:"")
                        intent.putExtra("SearchType","TB")
                        startActivity(intent)
                    }
                }
            }
        }
    }
    private inner class MyClassHolder(itemview:View):RecyclerView.ViewHolder(itemview){
        var imageView: ImageView?=null
        var tvName: TextView?=null
        init {
            imageView=itemview.rvitme_iv
            tvName=itemview.rvitme_tvName
        }
    }
}